package com.china08.yxyapi.api.manage.tec.teaching.scoreport.createexam;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.scoreport.Exam;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.scoreport.ExamRepository;
import com.china08.yxyapi.util.YxUserPointsUtilV3;

/**
 * 创建考试 保存
 * 
 */
@CrossOrigin
@RestController
public class CreateExamUpdateController extends YxyApiBaseController {

    @Autowired
    private ExamRepository examRepository;
    @Autowired
    private YxUserPointsUtilV3  yxUserPointsUtilV3;
    @RequestMapping(value = "/api_yw/manage/tec/teaching/scoreport/createexam/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String userId = getProfile(request, response).getId();//当前登录用户id
    	RespModel model = new RespModel();
    	Exam exam = new Exam();
    	if (reqModel.examId !=null && !reqModel.examId.equals("")) {
    		exam = examRepository.findById(reqModel.examId);
		}
    	boolean success = true;
        List<String>  examIdList = new ArrayList<String>();  
    	try {
    		List<ExamQues> questions = new ArrayList<ExamQues>();
            questions = reqModel.bigques; // 试题
            Exam.ExamQues examQues1 = null;
            List<Exam.ExamQues> questionsList = new ArrayList<Exam.ExamQues>();
            Integer order_num = 0;

            Integer bigQuesId = 0;// 大题号id
            if (questions.size() > 0) {
                for (ExamQues examQues : questions) {
                    bigQuesId += 1;
                    if (examQues.small != null && examQues.small.size() > 0) {
                        Integer smallQuesId = 0;// 小题号id
                        for (Small small : examQues.small) {
                            examQues1 = exam.new ExamQues();
                            smallQuesId += 1;
                            examQues1.section = bigQuesId; // 大题号id
                            examQues1.no = smallQuesId; // 小题号id
                            examQues1.sectionName = examQues.name; // 大题名称
                            examQues1.noName = small.name; // 小题名称
                            order_num += 1; // 小题序列号+1
                            examQues1.order_num = order_num;
                            examQues1.score = small.point; // 小题分数
                            questionsList.add(examQues1);
                        }
                    } else {
                        order_num += 1; // 大题序列号+1
                        examQues1 = exam.new ExamQues();
                        examQues1.section = bigQuesId; // 大题号id
                        examQues1.sectionName = examQues.name; // 大题名称
                        examQues1.noName = "";// 小题名称
                        examQues1.no = 0;// 小题号id
                        examQues1.score = examQues.point;
                        examQues1.order_num = order_num;
                        questionsList.add(examQues1);
                    }

                }
            }
            

            exam.setQuestions(questionsList);// 试题
            examRepository.save(exam);// 将试卷实体类进行保存
            
            examIdList.add(exam.getId());
            model.examIdList = examIdList;
    	} catch (Exception e) {
    		success = false;
		}
    	model.success  = success;
    	if (model.success) {
            yxUserPointsUtilV3.createExam(userId);
        }
        return model;
    }

    protected static class RespModel extends YxyApiRespModel {
    	//   
    	public List<String> examIdList;
    	//是否保存成功
    	public boolean success;
    }

    protected static class ReqModel extends YxyApiReqModel {
    	//考试id
    	public String examId;
        // 试题
        public List<ExamQues> bigques;
    }


    public static class ExamQues {
        public String name; // 大题名称
        public Integer point; // 分数
        public List<Small> small;// 小题集合
    }

    public static class Small {
        public String name;// 小题名称
        public Integer point;// 小题分数
    }
}
